const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeId:'1',
    
    hidden: true, //是否显示加载中，默认true,隐藏
  },
  changeItem(e){
    var arr = new Array();
    var id = e.currentTarget.dataset.id;
    var name  = e.currentTarget.dataset.name;
    var start_shop = { id: id, name: name }
    
    this.setData({
        activeId:id,
    })

    wx.setStorageSync('start_shop_', start_shop); 
    wx.switchTab({
      url: '../home/index',
    })
  },
    
  goPhone(e){
    wx.navigateTo({
      url: '../login/index'
    })
  },


//获取手机号
  clickphone: function (e) {
    var that = this;
    var session_key = wx.getStorageSync('userkey_');
    var userphone = wx.getStorageSync('userphone_');
    
    if (!session_key) {
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: app.https() + 'Weixin/authorization',
              header: {
                'content-type': 'application/json' // 默认值
              },
              data: {
                js_code: res.code
              },
              success: function (res) {
                var userkey = res.data.resData;
                e.userkey = userkey;
                console.log(res, 'login')
                wx.setStorageSync('userkey_', res.data.resData); //存储用户信息
                that.requese_get_phone(e)
              },
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
    } else {
      var userkey = session_key;
      e.userkey = userkey;
      that.requese_get_phone(e)
    }
    
  },
//获取电话事件
  requese_get_phone: function(e){
    var that = this;
    console.log(e)
          if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
            console.log('授权失败！')
          } else {
            // that.setData({
            //   hidden: false,
            // });
            //请求服务器解密
            wx.request({
              url: app.https() + 'Weixin/decryptPhone',
              header: {
                'content-type': 'application/x-www-form-urlencoded' // 默认值
              },
              data: {
                openid: e.userkey.openid,
                iv: e.detail.iv,
                encryptedData: e.detail.encryptedData,
                session_key: e.userkey.session_key,
              },
              method: "POST",
              success: function (res) {
                that.setData({
                  hidden: true,
                });
                if (res.data.resCode == 2001) {
                  console.log(res)
                  var start_shop = res.data.resData.shops[0];
                  wx.setStorageSync('start_shop_', start_shop);
                  wx.setStorageSync('all_start_shop_', res.data.resData);
                  wx.setStorageSync('userphone_', res.data.resData.phone); //存储用户信息
                  that.setData({
                    phone: res.data.resData.phone,
                    shops: res.data.resData.shops,
                    activeId: start_shop.id,
                  });
                } else if (res.data.resCode == 3001) {
                  console.log(res)
                  wx.setStorageSync('userphone_', res.data.resData.phone); //存储用户信息
                  that.setData({
                    phone: res.data.resData.phone,
                    shops: [],
                    activeId: '',
                  });

                }
              }
            })
          }
        

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userphone = wx.getStorageSync('userphone_');
    var shopid = wx.getStorageSync('start_shop_');
    console.log(shopid)
    that.setData({
      phone: userphone ? userphone : '',
    });
    wx.request({
      url: app.https() + 'index/getuserinfo',
      method: 'post',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        phone: userphone
      },
      success: function (res) {
        console.log(res)
        if (res.data.resCode == 2001) {
          wx.setStorageSync('all_start_shop_', res.data.resData);
          if (!shopid){
            var start_shop = res.data.resData.shops[0];
            var startid = start_shop? start_shop.id :'';
            wx.setStorageSync('start_shop_', start_shop);
          }else{
            var startid = shopid.id;
          }
          that.setData({
            shops: res.data.resData.shops ? res.data.resData.shops:[],
            activeId: startid,
          })
        } else if (res.data.resCode == 3001){
          wx.setStorageSync('userphone_', res.data.resData.phone); //存储用户信息
          that.setData({
            phone: res.data.resData.phone,
            shops: [],
            activeId: '',
          });
        }
        wx.stopPullDownRefresh();
      }
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // var shopid = wx.getStorageSync('start_shop_');
    // console.log(shopid)
    // if (shopid) {
    //   wx.switchTab({
    //     url: '../home/index',
    //   })
    // }
  },

  
  /**
    * 生命周期函数--监听页面显示
    */
  onShow: function (options) {
    var that =this
    var start_shop = wx.getStorageSync('start_shop_'); 
    var all_start_shop = wx.getStorageSync('all_start_shop_');
      if(start_shop){
        that.setData({
          shops: all_start_shop.shops,
          activeId: start_shop.id,
        })
      }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})